<template>
  <div v-loading="loading" class="role-manage">
    <!-- 按钮 -->
    <el-button type="primary" icon="el-icon-plus" size="small" @click="add">新增角色</el-button>
    <!-- 表格 -->
    <el-table style="margin:20px 0;" :data="tableData" border>
      <el-table-column type="index" label="序号" width="200" />
      <el-table-column prop="name" label="角色名" />
      <el-table-column prop="description" label="描述" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button type="text" @click="$refs.assign.show(row.id)">分配权限</el-button>
          <el-button type="text" @click="edit(row.id)">修改</el-button>
          <el-button type="text" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <!--
      current-page：绑定当前页
        默认情况下，你改了页码绑定的数据也不变
        加完sync修饰符，就可以让你页码改变数据也实时跟着变
      page-sizes：设置可选页容量
      page-size: 设置一页显示多少条
        默认情况下，改变了页容量，数据也不变
        加完sync修饰符，就可以让页容量改变数据也实时跟着变
      total：数据总量
     -->
    <el-pagination
      style="text-align:center;"
      :current-page.sync="pageInfo.page"
      :page-sizes="[2, 3, 5, 10]"
      :page-size.sync="pageInfo.pagesize"
      layout=" total,sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
      @current-change="getData"
      @size-change="getData"
    />

    <el-dialog :title="title" :visible.sync="show" @close="close">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="角色名称" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description" :label-width="formLabelWidth">
          <el-input v-model="form.description" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <assign-perm ref="assign" />
  </div>
</template>

<script>
import { roleListAPI, delRoleAPI, addRoleAPI, roleDetailAPI, editRoleAPI } from '@/api/role'
import assignPerm from './components/AssignPerm'
export default {
  components: {
    assignPerm
  },
  data() {
    return {
      tableData: [],
      loading: true,
      pageInfo: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      formLabelWidth: '120px',
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色名不能为空', trigger: 'blur' }
        ]
      },
      show: false,
      title: '新增角色'
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      // 发请求
      const res = await roleListAPI(this.pageInfo)
      this.tableData = res.data.rows
      this.pageInfo.total = res.data.total
      this.loading = false
    },

    del(id) {
      // 弹出确认框
      this.$confirm('是否删除该角色?')
        .then(async() => {
          // 调接口发请求
          await delRoleAPI(id)
          // 弹出提示
          this.$message.success('删除成功')

          if (this.tableData.length === 1 && this.pageInfo.page !== 1) {
            this.pageInfo.page--
          }
          // 重新加载数据
          this.getData()
        })
    },

    add() {
      this.title = '新增角色'
      this.show = true
    },

    async edit(id) {
      // 1. 发请求获取数据回显给表单
      const res = await roleDetailAPI(id)
      this.form = res.data
      // 2. 改标题
      this.title = '编辑角色'
      // 3. 把对话框显示出来
      this.show = true
    },

    save() {
      // 做整个表单校验
      this.$refs.form.validate(async v => {
        if (v) {
          if (this.title === '新增角色') {
            await addRoleAPI(this.form)
            this.$message.success('添加角色成功！')
          } else {
            // 修改
            await editRoleAPI(this.form)
            this.$message.success('修改角色成功！')
          }

          this.getData()
          this.show = false
        }
      })
    },

    close() {
      // 清空数据
      this.form = {
        name: '',
        description: ''
      }
      // 清空校验规则
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style>

</style>
